<template>
  <div>
    <el-form :model="blogForm" :rules="rules" ref="blogForm" label-width="100px">
      <el-form-item label="标题" prop="title" for="blog-title">
        <el-input v-model="blogForm.title" id="blog-title"></el-input>
      </el-form-item>

      <el-form-item label="摘要" prop="description" for="blog-description">
        <el-input type="textarea" v-model="blogForm.description" id="blog-description"></el-input>
      </el-form-item>

      <el-form-item label="内容" prop="content" style="min-height: 500px;max-height: 750px;">
        <mavon-editor v-model="blogForm.content"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('blogForm')">确认修改</el-button>
        <el-button @click="resetForm('blogForm')">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import * as constConfig from "../constData/ConstData";

export default {
  name: "BlogEdit",
  data() {
    return {
      blogForm: {
        id: '',
        title: '',
        description: '',
        content: ''
      },
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入摘要', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入内容', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log( this.blogForm)
          this.$axios.post(constConfig.prefixApi + "/blog/editOrUpdate", this.blogForm).then(() => {
            this.$alert("操作成功")
            this.$router.push({name: "Blogs", params: {blogId: this.blogForm.id}})
          }).catch(() => {
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const blogId = this.$route.params.blogId
    this.blogForm.id =blogId
    if (blogId !== "0") {
      this.$axios.get(constConfig.prefixApi + "/blog/" + blogId).then(res => {
        const blog = res.data.data
        this.blogForm.title = blog.title
        this.blogForm.description = blog.description
        this.blogForm.content = blog.content
      }).catch()
    }
  }
}
</script>

<style scoped>
.markdown-body {
  width: 100%;
  height: 100%;
}
</style>